Ext.namespace("Nippy.Components.NodeEditors");

Nippy.Components.NodeEditors.BasicEditor = Ext.extend(Ext.FormPanel, {
    constructor: function(config){
		this.nodeId = config.nodeId;
		this.parentId = config.parentId;
		Nippy.Components.NodeEditors.BasicEditor.superclass.constructor.apply(this, arguments);
    }
	,initComponent: function(){
		if (this.addOther){
			this.items = [
				{
		            fieldLabel: 'Внутреннее название',
		            name: 'Name',
		            allowBlank:false
	        	},{
		            fieldLabel: 'URL',
		            name: 'URL',
		            allowBlank: false
					,regex: /^[a-zA-Z0-9\-\/\.\(\)\+\=\~\?\&\%]+$/
					,regexText: "Только латинские буквы, цифры символы -()+/=~?&%"
		        },{
		            fieldLabel: 'Title',
		            name: 'Title',
		            allowBlank: false
		        },{
		            fieldLabel: 'META keywords',
		            name: 'MetaKeywords',
		            allowBlank: false
		        },{
		            fieldLabel: 'META description',
		            name: 'MetaDescription',
		            allowBlank: false
		        },{
		            fieldLabel: 'Публиковать в меню',
		            name: 'InMenu',
		            xtype: 'checkbox',
		            allowBlank: false,
		            inputValue: 1
		        },{
		            fieldLabel: 'Название для меню',
		            name: 'MenuTitle',
		            allowBlank: true
		        }
			].concat(this.items);
		}
		Ext.apply(this, {
		    frame:true,
		    bodyStyle:'padding:5px 5px 0',
		    defaults: {width: 630},
		    defaultType: 'textfield',
		    closable: true,
		    autoScroll: true,
		
		    buttons: [{
		        text: 'Сохранить',
		        scope: this,
		        handler: this.saveClick
		    }
		    ,{
		    	text: 'Закрыть',
		    	scope: this,
		    	handler: this.closeClick
		    }
		    ]
		});
		Nippy.Components.NodeEditors.BasicEditor.superclass.initComponent.apply(this, arguments);
		this.addEvents('onsaved', 'onclosing','onfailed');
	}
	/*,saveClick: function(){
		
	}*/
	,closeClick: function(){
		this.fireEvent('onclosing',this);
	}
	,submitFailure: function(form, action) {
		switch (action.failureType) {
		case Ext.form.Action.CLIENT_INVALID:
			this.fireEvent('onfailed',this,'Form fields may not be submitted with invalid values');
			break;
		case Ext.form.Action.CONNECT_FAILURE:
			this.fireEvent('onfailed',this,'Ajax communication failed');
			break;
		case Ext.form.Action.SERVER_INVALID:
			this.fireEvent('onfailed',this,action.result.msg);
			break;
		}
	}
	,fillForm: function(o){
		var f = this.getForm();
		for(var k in o) {
			if (typeof(k)=='undefined') continue;
			var fl = f.findField(k);
			if (fl != null) 
			{
				var x = fl.getXType();
				if ('checkbox'==x)
					fl.setValue(o[k]);
				else if ('combo'==x)
					fl.setValue(o[k]);
				else if ('htmleditor'==x)
					fl.setValue(o[k]);
				else fl.setRawValue(o[k]);
			}
		}
		return f;
	}
});